package com.spring.sunflower.widget

import android.content.Context
import android.content.Intent
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.spring.sunflower.R
import com.spring.sunflower.common.WebViewActivity
import com.spring.sunflower.constant.Constant

/**
 * Created by Kevin on 2021/7/28<br/>
 *
 * Describe:<br/>
 */
class BottomPayDialog(context: Context) : BottomPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.layout_bottom_pay_dialog
    }

    override fun onCreate() {
        super.onCreate()
        var rlAliPay = findViewById<RelativeLayout>(R.id.rlAliPay)
        var rlWeChatPay = findViewById<RelativeLayout>(R.id.rlWeChatPay)
        var cbAliPay = findViewById<CheckBox>(R.id.cbAliPay)
        var cbWeChatPay = findViewById<CheckBox>(R.id.cbWeChat)
        var llPay = findViewById<LinearLayout>(R.id.ll_pay_container)
        val tvAgreement = findViewById<TextView>(R.id.tvAgreement)
        val checkBox = findViewById<CheckBox>(R.id.checkBox)
        rlAliPay.setOnClickListener {
            cbWeChatPay.isChecked = false
            cbAliPay.isChecked = true
        }
        cbAliPay.setOnClickListener {
            cbWeChatPay.isChecked = false
            cbAliPay.isChecked = true
        }
        rlWeChatPay.setOnClickListener {
            cbWeChatPay.isChecked = true
            cbAliPay.isChecked = false
        }
        cbWeChatPay.setOnClickListener {
            cbWeChatPay.isChecked = true
            cbAliPay.isChecked = false
        }
        llPay.setOnClickListener {
            if (checkBox.isChecked) {
                if (cbAliPay.isChecked) {
                    listener?.onPaySelect(0)
                } else {
                    listener?.onPaySelect(1)
                }
            } else {
                ToastUtils.showShort("请阅读并同意支付协议")
            }
        }
        tvAgreement.setOnClickListener {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "支付协议")
            intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.PAY_AGREEMENT)
            context.startActivity(intent)
        }

    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context) * .8f).toInt()
    }

    interface OnPaySelectListener {
        fun onPaySelect(position: Int)
    }

    var listener: OnPaySelectListener? = null
    open fun setOnPaySelectListener(l: OnPaySelectListener) {
        listener = l
    }

}